package com.woniuxy.woniufp.bank.config;

import com.woniuxy.woniufp.common.model.constant.MQConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@Slf4j
public class RabbitConfig {
    /*
      手动触发ConnectionListener 便可解决这个问题
      因为在rabbitMQ采用的是懒加载， connection 启动时不会创建，
      因此需要创建一个连接，通过 ConnectionFactory接口 调用createConnection()方法创建一个连接注册到容器中
     */
    @Bean
    ApplicationRunner runner(ConnectionFactory cf) {
        log.info("执行成功");
        return args -> cf.createConnection().close();
    }
    //1.声明交换机
    @Bean
    DirectExchange directExchange() {
        System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
        return new DirectExchange(MQConstant.WONIUFP_EXCHANGE_ACCOUNT,true,false);
    }
    //2.声明队列
    @Bean
    public Queue queue() {
        System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
        return new Queue(MQConstant.WONIUFP_QUEUE_ACCOUNT,true);
    }
    //3. 交换机绑定队列
    @Bean
    Binding bindingDirect1() {
        return BindingBuilder.bind(queue()).to(directExchange()).with(MQConstant.WONIUFP_ACCOUNT_ROUTING);
    }
}
